package com.nami.web.controller.user;

import cn.hutool.core.util.RandomUtil;
import com.nami.store.api.request.user.UserAddRequest;
import com.nami.store.api.service.RemoteUserService;
import com.nami.web.dto.user.UserAddDTO;
import com.nami.web.dto.user.UserLoginDTO;
import com.nami.web.facade.UserFacadeService;
import com.nami.web.utils.ChineseNameUtil;
import com.nami.web.utils.RandomUtils;
import com.nami.web.vo.user.UserInfoVO;
import com.nami.web.vo.user.UserLoginVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.domain.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * APP - 用户
 *
 * @author lbc
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {

    @DubboReference
    RemoteUserService userService;

    private final UserFacadeService userFacadeService;


    @GetMapping("/info")
    public R<UserInfoVO> getInfo(String userId) {
        return R.ok(userFacadeService.getUserById(userId));
    }

    @PostMapping("/login")
    public R<UserLoginVO> login(@Validated @RequestBody UserLoginDTO loginDTO) {
        return R.ok(userFacadeService.login(loginDTO));
    }

    @PostMapping("/register")
    public R<Boolean> register(@Validated @RequestBody UserAddDTO addDTO) {
        return R.ok(userFacadeService.register(addDTO));
    }

    /**
     * 随机生成用户
     *
     * @param
     * @return
     */
    @GetMapping("/genRandomUser")
    public R<Boolean> randomINewUsers() {

        List<UserAddRequest> requests = new ArrayList<>();
//        for (int i = 7; i < 3000; i++) {
//            // register
//            UserAddRequest userInfo = new UserAddRequest();
//            userInfo.setUserId(i + "");
//            userInfo.setNickname(ChineseNameUtil.randomName());
//            userInfo.setAvatarUrl("https://file.lwoowl.cn/avatar_jgd/55e321a8-645a-47ae-99ae-284a70086b95.png");
//            userInfo.setAppKey("test");
//            userInfo.setUserType(10);
//            userInfo.setMobile(RandomUtil.randomMobile());
//            userInfo.setStatus(10);
//            userInfo.setUserType(10);
//            requests.add(userInfo);
//        }
        for (int i = 10; i < 3000; i++) {
            // register
            UserAddRequest userInfo = new UserAddRequest();
            userInfo.setUserId(i + "");
            userInfo.setNickname(ChineseNameUtil.randomName());
            userInfo.setRealName(userInfo.getNickname());
            userInfo.setAvatarUrl("https://file.lwoowl.cn/avatar_jgd/55e321a8-645a-47ae-99ae-284a70086b95.png");
            userInfo.setAppKey("test");
            userInfo.setUserType(10);
            userInfo.setMobile(RandomUtils.randomMobile());
            userInfo.setAccount(userInfo.getMobile());
            userInfo.setStatus(10);
            userInfo.setUserType(10);

            userInfo.setGender(1);
            if (i / 2 == 0) {
                userInfo.setGender(2);
            }

            userInfo.setSalt(RandomUtil.randomString(6));
            userInfo.setCreateTime(new Date());
            userInfo.setUpdateTime(new Date());
            requests.add(userInfo);
        }

        userService.batchSave(requests);
        return R.ok();
    }


}
